package com.china08.yxyapi.api.home.activity.weike;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.huodong.YxyHuodongWeikeContestCatalogue;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.db.res.YxyResWeikeKnow;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyHuodongWeikeContestCatalogueRepository;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.db.YxyResWeikeKnowRepository;

@CrossOrigin
@RestController
public class ActivityDelContentController extends YxyApiBaseController {

    @Autowired
    private YxyHuodongWeikeContestCatalogueRepository yxyHuodongWeikeContestCatalogueRepository;

    @Autowired
    private YxyResResourceRepository yxyResResourceRepository;

    @Autowired
    private YxyResWeikeKnowRepository weikeKnowRepository;

    @RequestMapping(value = "/api_yw/home/activity/tea/del", method = RequestMethod.DELETE, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel process(//
            @RequestParam(value = "id", required = true) String id, HttpServletRequest request, //
            HttpServletResponse response//
    ) {
        RespModel respModel = new RespModel();
        YxyHuodongWeikeContestCatalogue one = yxyHuodongWeikeContestCatalogueRepository.findOne(id);
        if (one != null) {
            yxyHuodongWeikeContestCatalogueRepository.delete(one);
            if (one.getStatus()) {
                YxyResResource resource = yxyResResourceRepository.findById(one.getWeikeResId());
                resource.setIsDelete(1);
                List<YxyResWeikeKnow> list = weikeKnowRepository.findByWeikeId(one.getWeikeResId());
                for (YxyResWeikeKnow yxyResWeikeKnow : list) {
                    yxyResWeikeKnow.setWeiKeStatus(0);
                    weikeKnowRepository.save(yxyResWeikeKnow);
                }
                yxyResResourceRepository.save(resource);
            }
        }
        return respModel;
    }

    protected static class RespModel extends YxyApiRespModel {
    }

}
